// pages/shezhi/jiaoyishezhi/jiaoyishezhi.js
const utilbase = require('../../../../../utils/setpwdbase64');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    yanzm: '',
    mima1: '',
    mima2: '',
    yzmTime: 60,
  },
  baoCun() {
    let that = this
    var telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
    var inputStr = that.data.phone
    if (!(telStr.test(inputStr))) {
      wx.showToast({
        title: '手机号码输入不规范',
        icon: 'none'
      })
      return
    }
    if (!that.data.yanzm) {
      wx.showToast({
        title: '验证码不能为空',
        icon: 'none'
      })
      return
    }
    if (that.data.mima1 != that.data.mima2) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none'
      })
      return
    }
    if (!that.data.mima1) {
      wx.showToast({
        title: '密码不能为空',
        icon: 'none'
      })
      return
    }
    if (that.data.mima1.length < 6) {
      wx.showToast({
        title: '密码不能少于6位',
        icon: 'none'
      })
      return
    }
    setTimeout(function() {
      wx.showLoading({
        title: '正在提交',
      })
    }, 0)
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "tPassword",
        type: 0,
        userId: wx.getStorageSync('userId'),
        userPhone: that.data.phone,
        smsCode: that.data.yanzm,
        npassword: utilbase.base64encode(utilbase.utf16to8(that.data.mima1)),
        cpassword: utilbase.base64encode(utilbase.utf16to8(that.data.mima2))
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        setTimeout(function() {
          wx.hideLoading()
        }, 0)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1500
          })
        }, 0)
        console.log(res)
        if (res.data.result == 1) {
          return
        } else {
          wx.setStorageSync("paypasswordtype", 2);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      }
    });
  },
  getmima2(e) {
    this.setData({
      mima2: e.detail.value
    })
  },
  getmima1(e) {
    this.setData({
      mima1: e.detail.value
    })
  },
  getphone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  getyanzm(e) {
    this.setData({
      yanzm: e.detail.value
    })
  },
  huoQu() {
    let that = this
    var telStr = /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
    var inputStr = that.data.phone
    if (!(telStr.test(inputStr))) {
      wx.showToast({
        title: '手机号码输入不规范',
        icon: 'none'
      })
      return
    }
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "phoneMSCode",
        userPhone: that.data.phone
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        setTimeout(function() {
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none'
          })
        }, 0)
      }
    });
    var interval = setInterval(function timestampToTime() {
      var date = that.data.yzmTime
      if (date <= 1) {
        that.setData({
          yzmTime: 60
        })
        clearInterval(interval)
      } else {
        date--
        that.setData({
          yzmTime: date
        })
        // console.log(date)
      }
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      phone: wx.getStorageSync('telephone'),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})